package com.cgah

import grails.test.*
import com.cgah.Customer

class CustomerTests extends GrailsUnitTestCase {

   Customer bc;
   protected void setUp() {
        super.setUp()
        // mockForConstraintsTests(Customer);
        mockDomain(Customer);
        bc = new Customer( username: "passwordtester", emailAddress: "asdf@asdf.com", createdDate: new Date(), password: "12312312");
    }

    protected void tearDown() {
        super.tearDown()
    }

    // Done C-4: Password must be between 6-8 characters (integration test)
    void testValidatePassword() {
        bc.password = "12345";
        assertFalse("bad password ", bc.validate());

        bc.password = "123456789";
        assertFalse("bad password ", bc.validate());

        bc.password = "12345678";
        assertTrue("good password ", bc.validate());

        bc.password = "1234567";
        assertTrue("good password ", bc.validate());

        bc.password = "123456";
        assertTrue("good password ", bc.validate());
    }

    // how do you test that an email address is NOT of the form valid form (*@*.*)?
    // Done C-3: Email address must be of a valid form (*@*.*) (integration test)

    void testValidateEmailAddress() {
        bc.emailAddress = "asdfadsf";
        assertFalse("bad email address ", bc.validate());

        bc.emailAddress = "asdf@";
        assertFalse("bad email address ", bc.validate());

        bc.emailAddress = "asdf@asdf";
        assertFalse("bad email address ", bc.validate());

        bc.emailAddress = "asdf@asd.com";
        assertTrue("good email address ", bc.validate());
    }

    //Done C-2: Email address must be a unique field (integration test)

    void testEmailAddressUnique() {
        // bc.save(flush:true);
        def a = new Customer(username: "nameone",  emailAddress: "asdf@asdf.com", createdDate: new Date(), password: "asdfasdfasdfsadf");
        def c = new Customer(username: "nametwo", emailAddress: "asdf@asdf.com", createdDate: new Date(), password: "asdfasdfasdfsadf");
        // mockDomain(Customer, [a, c])
        a.save(flush: true);
        assertFalse("must have unique email address", c.validate());
    }

    void testFieldsExist() {
        def c = new Customer(username: "exitsone", emailAddress: "asdf@asdf.com", createdDate: new Date(), password: "asdfasdfasdfsadf");
        assertNotNull(c);
    }

    void testCustomerExists() {

        Customer c = Customer.get(98766)
        if (c) {
            println "c is: " + c;
        } else {
            println "c is no good: " + c;
        }
    }
}
